import { createStore } from "vuex";
import * as api from "@/api/index.js";
export default createStore({
  state: {
    playlist: [
      {
        name: "",
        id: "",
        al: {
          id: "",
          name: "",
          pic: "",
          picUrl: "",
          pic_str: "",
        },
      },
    ],
    playCurrentIndex: 0,
    lyric: "",
    currentTime: 0,
    intervalId: 0,
    timer: null,
    audio:null
  },
  getters: {
    lyricList(state) {
      // 设置格式数据
      let arr = state.lyric.split(/\n/gis).map((item, i) => {
        let mill = ''
        let lyric = ''
        if(!isNaN(Number(item[9]))){
          mill = parseInt(item.slice(7, 10));
          lyric = item.slice(11, item.length);
        }else{
          mill = parseInt(item.slice(7, 9));
          lyric = item.slice(10, item.length);
        }
        let min = parseInt(item.slice(1, 3));
        let sec = parseInt(item.slice(4, 6));
        let time = mill + sec * 1000 + min * 60 * 1000+200;
        return { min, sec, mill, lyric, time, item };
      });
      for (let i = 1; i < arr.length; i++) {
        if (i == 0) {
          arr[i].pre = 1;
        } else {
          if (i < arr.length) {
            arr[i-1].pre = arr[i].time;
          }
        }
      }
      console.log(arr,7777)
      return arr;
    },
  },
  mutations: {
    setPlaylist(state, value) {
      state.playlist = value;
    },
    setPlayIndex(state, value) {
      state.playCurrentIndex = value;
    },
    setLyric(state, value) {
      state.lyric = value;
    },
    setCurrentTime(state, value) {
      state.currentTime = value;
    },
    setCurAudio(state,value){
      state.audio = value
    },
    setchangeSongs(state,value){
      clearInterval(state.timer)
      state.currentTime = 0
      state.audio.currentTime = 0
      // console.log(this.playlist, this.playCurrentIndex, 88888);
      if (value == -1) {
        if (state.playCurrentIndex == 0) {
          state.playCurrentIndex = state.playlist.length - 1;
        } else {
          state.playCurrentIndex = state.playCurrentIndex - 1;
        }
      } else if (value == 1) {
        if (state.playCurrentIndex == state.playlist.length - 1) {
          state.playCurrentIndex = 0;
        } else {
          state.playCurrentIndex = state.playCurrentIndex * 1 + 1;
        }
      }else if (value== 0){

        state.playCurrentIndex =JSON.parse(JSON.stringify(state.playCurrentIndex))
      }
      console.log(state.playlist, value, state.playCurrentIndex, 999999);
      // 更新选中的音乐

    }
  },
  actions: {
    async reqLyric(context, payload) {
      let res = await api.getLyric(payload.id);
      context.commit("setLyric", res.data.lrc.lyric);
    },
  },
  modules: {},
});
